<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="联系人" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="归属部门" prop="deptId">
        <treeselect style="width:300px;" v-model="queryParams.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['gongyuan:personal:add']">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="登录用户名" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          {{scope.row.sysUser.userName}}
        </template>
      </el-table-column>
      <el-table-column label="姓名" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          {{scope.row.sysUser.nickName}}
        </template>
      </el-table-column>
      <el-table-column label="联系方式" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.sysUser.phonenumber}}
        </template>
      </el-table-column>
      <el-table-column label="部门" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.sysUser.dept.deptName}}
        </template>
      </el-table-column>
      <el-table-column label="是否禁用" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.status === '0' ? '否' : '是'}}
        </template>
      </el-table-column>
      <el-table-column label="分配权重" prop="servicePersonaProportion" :show-overflow-tooltip="true" />
      <!-- <el-table-column label="服务次数" prop="" :show-overflow-tooltip="true" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-info" @click="handleDetail(scope.row)" v-hasPermi="['gongyuan:personal:byid']">详情</el-button>
          <el-button class="gongyuan-update" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasPermi="['gongyuan:personal:edit']">修改</el-button>
          <el-button class="gongyuan-delete" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['gongyuan:personal:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import CityPicker from '@/components/cityPicker'
import { treeselect } from '@/api/system/dept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { createCURD } from '@/api/sys'
const fetchApi = createCURD('personal')
export default {
  name: 'Personallist',
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      list: [],
      total: 0,
      // 查询参数
      queryParams: {
        nickName: undefined,
        deptId: undefined,
        pageNum: 1,
        pageSize: 10
      },
      deptOptions: []
    }
  },
  created() {
    this.getTreeselect()
    this.getList()
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data
      })
    },
    async initDictsOptions(dicts, listProps) {
      try {
        const res = await this.getDicts(dicts)
        if (res.code === 200) {
          this[listProps] = res.data.map((per) => ({
            label: per.dictLabel,
            value: per.dictValue
          }))
        }
      } catch (error) {
        console.log(error)
      }
    },
    formatter(prop) {
      return row[prop]
    },
    async getList() {
      this.loading = true
      const res = await fetchApi.list(this.queryParams)
      this.list = res.rows
      this.total = res.total
      this.loading = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    handleAdd() {
      // 此处后台管理添加的路由, path对应组件name须一致,不可再修改
      this.$router.push({ name: 'Personaledit' })
    },

    handleDetail(row) {
      this.$router.push({ name: 'Personaldetail', query: { id: row.servicePersonalId } })
    },
    handleEdit(row) {
      this.$router.push({ name: 'Personaledit', query: { id: row.servicePersonalId } })
    },
    handleDelete(row) {
      this.$confirm('确认删除?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function () {
          return fetchApi.del(row.servicePersonalId)
        })
        .then(() => {
          this.queryParams.pageNum = 1
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function () {})
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/ .vue-treeselect__control {
  height: 30px;
  line-height: 32px;
  margin-top: 2px;
}
</style>
